<template>
  <div class="cart">
    <h2>购物车</h2>
    <div v-for="item in cartItems" :key="item.id" class="cart-item">
      {{ item.name }} - ¥{{ item.price }} - 数量: {{ item.quantity }}
      <button @click="increment(item.id)">+</button>
      <button @click="decrement(item.id)">-</button>
      <button @click="remove(item.id)">删除</button>
    </div>
    <div class="total">总价: ¥{{ cartTotal }}</div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'

export default {
  computed: {
    ...mapGetters('cart', ['cartItems', 'cartTotal'])
  },
  methods: {
    ...mapActions('cart', [
      'incrementQuantity',
      'decrementQuantity',
      'removeFromCart'
    ]),
    increment(id) {
      this.incrementQuantity(id)
    },
    decrement(id) {
      this.decrementQuantity(id)
    },
    remove(id) {
      this.removeFromCart(id)
    }
  }
}
</script>

<style>
.cart {
  border: 1px solid #ddd;
  padding: 20px;
  margin: 20px;
}
.cart-item {
  margin: 10px 0;
}
button {
  margin-left: 10px;
}
.total {
  font-weight: bold;
  margin-top: 20px;
}
</style>